<?php
namespace Home\Controller; 

class IndexController extends HomeController {
    public function index(){
        $this->display();
    }
    
    public function test(){
        $xmldata = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<INSUREQRET>
    <MAIN>
        <SERIALDECIMAL>07cc7d9e6523c4163fd5377ea01a963b</SERIALDECIMAL>
        <TRANSRDATE>2015-08-30 01:50:24</TRANSRDATE>
        <RESULTCODE>0000</RESULTCODE>
        <ERR_INFO>交易成功</ERR_INFO>
    </MAIN>
    <VHLMODEL_LIST>
        <VHLMODEL>
            <C_VEHICLE_CODE>DZAAVD0108</C_VEHICLE_CODE>
            <C_MODEL_DESC>新车价,轿车,双离合 时尚型 国Ⅳ</C_MODEL_DESC>
            <C_VEHICLE_BRAND>一汽大众</C_VEHICLE_BRAND>
            <C_VEHICLE_NAME>大众FV7146FBDGG</C_VEHICLE_NAME>
            <C_VEHICLE_FAMILY>速腾</C_VEHICLE_FAMILY>
            <C_IMPORT_FLAG>合资</C_IMPORT_FLAG>
            <N_LIMIT_LOAD_PERSON>5</N_LIMIT_LOAD_PERSON>
            <C_WHOLE_WEIGHT>0</C_WHOLE_WEIGHT>
            <N_VEHICLE_TONNAGE>0.0</N_VEHICLE_TONNAGE>
            <C_EXT_MSR>1390</C_EXT_MSR>
            <C_MARKET_TIMESTAMP>201309</C_MARKET_TIMESTAMP>
            <N_VEHICLE_PRICE>160400.0</N_VEHICLE_PRICE>
        </VHLMODEL>
        <VHLMODEL>
            <C_VEHICLE_CODE>DZAAVD0108</C_VEHICLE_CODE>
            <C_MODEL_DESC>新车价,轿车,双离合 时尚型 国Ⅳ</C_MODEL_DESC>
            <C_VEHICLE_BRAND>一汽大众</C_VEHICLE_BRAND>
            <C_VEHICLE_NAME>大众FV7146FBDGG</C_VEHICLE_NAME>
            <C_VEHICLE_FAMILY>速腾</C_VEHICLE_FAMILY>
            <C_IMPORT_FLAG>合资</C_IMPORT_FLAG>
            <N_LIMIT_LOAD_PERSON>5</N_LIMIT_LOAD_PERSON>
            <C_WHOLE_WEIGHT>0</C_WHOLE_WEIGHT>
            <N_VEHICLE_TONNAGE>0.0</N_VEHICLE_TONNAGE>
            <C_EXT_MSR>1390</C_EXT_MSR>
            <C_MARKET_TIMESTAMP>201309</C_MARKET_TIMESTAMP>
            <N_VEHICLE_PRICE>160400.0</N_VEHICLE_PRICE>
        </VHLMODEL>
    </VHLMODEL_LIST>
</INSUREQRET>';
       $xml = new \SimpleXmlIterator($xmldata);
        $nameSpaces = $xml->getNamespaces(true);
        $output = $this->xmlNameSpaceToArray($xml,$nameSpaces);
        print_r($output);
    }
    
    protected function xmlNameSpaceToArray(\SimpleXmlIterator $xml, $nameSpaces=Null){
          $preparedArray = array();
          for($xml->rewind(); $xml->valid(); $xml->next()) {
                $key = $xml->key();
                if(!isset($preparedArray[$key])) {
                    $preparedArray[$key] = array(); 
                    $i=0; 
                }else{
                    $i = count($preparedArray[$key]);
                }
                $simple = true;
                foreach($xml->current()->attributes() as $k=>$v) {
                    $preparedArray[$key][$i][$k]=(string)$v;
                    $simple = false;
                }
                if($nameSpaces) {
                    foreach($nameSpaces as $nid=>$name) {
                        foreach($xml->current()->attributes($name) as $k=>$v) {
                            $preparedArray[$key][$i][$nid.':'.$k]=(string)$v;
                            $simple = false;
                        }
                    }
                } 
                if($xml->hasChildren()) {
                    if($simple){
                        $preparedArray[$key][$i] = $this->xmlNameSpaceToArray($xml->current(), $nameSpaces);
                    }else{
                        $preparedArray[$key][$i]['content'] = $this->xmlNameSpaceToArray($xml->current(), $nameSpaces);
                    }
                } else {
                    if($simple){
                        $preparedArray[$key][$i] = strval($xml->current());
                    }else{
                        $preparedArray[$key][$i]['content'] = strval($xml->current());
                    }
                }
                $i++;
          }
          foreach($preparedArray as $key => &$value){
              if(is_array($value) && count($value)==1 && isset($value[0])){
                  $value = $value[0];
              }
          }
          return $preparedArray;
    }

}